/**
 * 
 */
package slhs.tempo.TableAnalyzer;

import javax.swing.table.TableModel;

/**
 * TableModelDataConverter.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class TableModelDataConverter {

    private TableModel formattedTableModel;

    public TableModelDataConverter(TableModel unformattedTableModel) {
	this.formattedTableModel = unformattedTableModel;
    }

    public void startConversion() {
	int rowCount = formattedTableModel.getRowCount();
	int columnCount = formattedTableModel.getColumnCount();
	for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
	    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
		Object unformattedCellValue = formattedTableModel.getValueAt(rowIndex, columnIndex);

		try {
		    // try converting the current cell value to integer and add to new model
		    Integer numberFormattedCellValue = Integer.valueOf(unformattedCellValue.toString());
		    formattedTableModel.setValueAt(numberFormattedCellValue, rowIndex, columnIndex);
		} catch (Exception e) {
		    // else keep it a string
		    formattedTableModel.setValueAt(unformattedCellValue, rowIndex, columnIndex);
		}
	    }
	}

    }

    public TableModel getDataConvertedModel() {
	return formattedTableModel;
    }

}
